<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        :root {
            --transition: 0.1s;
            --border-radius: 56px;
        }

        .button-wrap {
            width: 300px;
            height: 100px;
            position: relative;
            transition: transform var(--transition), box-shadow var(--transition);
        }

        .button-wrap.pressed {
            /* transform: translateZ(0) scale(0.95); */
            width: 100%;
            height: 100%;
        }

        .button-wrap.pressed .button {
            box-shadow: 0px -1px 1px rgba(0, 0, 0, 0.5), 0px 1px 1px rgba(0, 0, 0, 0.5);
        }

        .button {
            width: 100%;
            height: 100%;
            border: 1px solid #fff;
            overflow: hidden;
            border-radius: var(--border-radius);
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.25), 0px 8px 16px rgba(0, 0, 0, 0.15),
                0px 16px 32px rgba(0, 0, 0, 0.125);
            transform: translateZ(0);
            cursor: pointer;
        }

        .text {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            pointer-events: none;
            color: rgba(0, 0, 0, 0.7);
            font-size: 30px;
            font-weight: 500;
            text-shadow: 0px -1px 0px rgba(255, 255, 255, 0.5), 0px 1px 0px rgba(255, 255, 255, 0.5);
        }

        .text::selection {
            background-color: transparent;
        }

        .button .button-reflection {
            width: 100%;
            height: 100%;
            transform: scaleX(-1);
            object-fit: cover;
            opacity: 0.7;
            filter: blur(2px) saturate(0.6) brightness(1.1);
            object-position: 0 -100px;
        }
    </style>
</head>

<body>
    <div class="button-wrap" id="btn">
        <div class="button">
            <video class="button-reflection" id="video" src=""></video>
        </div>
        <div class="text">前端大佬</div>
    </div>

    <script>
        /* 
            参考链接：https://mp.weixin.qq.com/s/1k3xpLpvcW45nt1wc3rK9g
        */

        // navigator.mediaDevices.getUserMedia({
        //     video: true,
        //     audio: false,
        // })
        //     .then((stream) => {
        //         const video = document.getElementById('video');
        //         video.srcObject = stream;
        //         video.onloadedmetadata = () => {
        //             video.play();
        //         };
        //     })
        //     .catch((e) => console.log(e));

        const btn = document.querySelector('#btn')
        const video = document.querySelector('#video')
        const text = document.querySelector('.text')

        btn.addEventListener('click', function () {
            const res = this.classList.toggle('pressed')
            // console.log(res)
            text.innerHTML = res ? '大佬在此' : '前端大佬'
        })

        // async await
        async function videoPlay() {
            try {
                const stream = await navigator.mediaDevices.getUserMedia({
                    video: true,
                    audio: false,
                })

                video.srcObject = stream;
                video.onloadedmetadata = () => {
                    video.play();
                };
            } catch (e) {
                console.log(e.message)
                if (e.message === 'Permission denied') {
                    location.reload(true)
                    return alert('请选择打开摄像机')
                }
                alert('无法打开摄像机')
                location.reload(true)
            }
        }
        videoPlay()
    </script>
</body>

</html>